#include "ToolController.h"

//==============================================================================
// Public Interface
//==============================================================================

/**		
 *	Sets the active tool as specified by the parameter
 */
void ToolController::setSubdivider(Subdivider* scheme)
{
	this->subdivider = scheme;
}


/**		
 *	Sets the active tool as specified by the parameter
 */
void ToolController::setActiveTool(Tool aTool)
{
	ModelFrame* currentFrame;

	this->activeTool = aTool;

	// set the selectable mesh based on the tool
	currentFrame = ModelLocator::instance()->getFrame();
	if( currentFrame )
	{
	switch (this->activeTool)
	{
		case CONNECTIVITY_INSPECT:
		{
			currentFrame->selectable = ModelFrame::COARSE;
			break;
		}
		case WEIGHT_INSPECT:
		{
			currentFrame->selectable = ModelFrame::RECONNECTED;
			break;
		}
		case SHARP_FEATURES:
		{
			currentFrame->selectable = ModelFrame::COARSE;
			break;
		}
		default: return;
	}
	}
}


/**		
 *	Performs the function of the currently active tool
 */
void ToolController::useActiveTool()
{
	if (this->activeTool == NULL  ||  this->selection == NULL)
	{
		return;
	}

	switch (this->activeTool)
	{
		case SUBDIVISION:
		{
			doSubdivision();
			break;
		}
		case CONNECTIVITY_INSPECT:
		{
			doConnectivityInspection();
			break;
		}
		case WEIGHT_INSPECT:
		{
			doWeightInspection();
			break;
		}
		case SHARP_FEATURES:
		{
			doSharpEdgeMarking();
			break;
		}
		default: return;
	}
}


/**		
 *	Sets the current selection as specified in the parameter
 */
void ToolController::setSelection(const Selection* selection)
{
	this->selection = selection;
	useActiveTool();
}


//==============================================================================
// Helper Methods (private)
//==============================================================================

/**		
 *	Creates a file open dialog and opens/loads a mesh file into
 *	a new ModelFrame object.
 */
ModelFrame* ToolController::openMeshFile(QWidget* dialogParent)
{
	IO::Options		opt;
	ModelFrame*		newFrame;
	QString			qFilename;
	PolyMesh*		origMesh;
	ModelLocator*	modelLocator;

	//Get the filename with the File Open dialog...

	newFrame  = new ModelFrame();
	qFilename = QFileDialog::getOpenFileName(
		    dialogParent,
			tr("Open mesh file"),
			tr(""),
			tr("OBJ Files (*.obj);;"
			"OFF Files (*.off);;"
			"STL Files (*.stl);;"
			"All Files (*)"));

	if (qFilename.isEmpty() || qFilename.isNull())
	{	
		return NULL;  // canceled or empty file
	}

	//Read in the new coarse mesh...

	origMesh = new PolyMesh();
	if ( IO::read_mesh( *origMesh, string(qFilename.toLocal8Bit()), opt ) )
	{
		// create coarse mesh and subdivide it in advance
		newFrame->loadOptions = opt;
		newFrame->coarse      = new PolyMesh(*origMesh);
		newFrame->reconnected = NULL;
		newFrame->subdivided  = NULL;
		
		return newFrame;
	}

	return NULL;
}

/**		
 *	Performs subdivision on the current selection.
 */
void ToolController::doSubdivision()
{

}


/**		
 *	Performs connectivity inspection on current selection.
 */
void ToolController::doConnectivityInspection()
{
	if (this->selection != NULL)
	{
		this->subdivider->showConnectivity( this->selection );
	}
}


/**		
 *	Performs weight inspection on current selection.
 */
void ToolController::doWeightInspection()
{
	if (this->selection != NULL)
	{
		this->subdivider->showWeights(this->selection);
		ModelLocator::instance()->getFrame()->animated = *(ModelLocator::instance()->getFrame()->reconnected);
	}

}


/**		
 *	Performs sharp edge marking on current selection.
 */
void ToolController::doSharpEdgeMarking()
{
	if (this->selection != NULL)
	{
		this->subdivider->showConnectivity( this->selection );
		ModelLocator::instance()->getFrame()->animated = *(ModelLocator::instance()->getFrame()->reconnected);
	}
}